<template>
  <view class="login-container">
    <view class="head" style="background-color: #ffcc00; height: 180rpx; display: flex; align-items: center; justify-content: flex-start; position: fixed; top: 0; left: 0; right: 0; z-index: 999;">
      <img
        style="height: 50rpx; width: 50rpx; padding: 10rpx; margin-left: 20rpx;"
        src="../../static/tabbar/返回.png"
        alt=""
        @click="handleReturn"
      />
      <view style="font-size: 32rpx; margin-left: 20rpx;margin-top:10rpx">
        护工登录
      </view>
    </view>
    <view class="form-container">
      <!-- 图片 -->
      <view class="image-container">
        <img 
          src="https://cloud-minapp-47896.cloud.ifanrusercontent.com/1szyLMpr62t9eih2.png" 
          class="profile-image"
        />
      </view>
      <!-- 登录表单 -->
      <view class="form-item">
        <input 
          v-model="loginForm.userName" 
          placeholder="护工编号" 
          class="input"
        />
      </view>
      <view class="form-item">
        <input 
          v-model="loginForm.password" 
          placeholder="密码默认12345678" 
          password 
          class="input"
        />
      </view>
      <button class="login-btn" @click="handleLogin">登录</button>

      <!-- 注册表单 -->
      <!-- <view class="title" style="margin-top: 40rpx;">用户注册</view>
      <view class="form-item">
        <input 
          v-model="registerForm.userName" 
          placeholder="请输入用户名" 
          class="input"
        />
      </view>
      <view class="form-item">
        <input 
          v-model="registerForm.password" 
          placeholder="请输入密码" 
          password 
          class="input"
        />
      </view>
      <button class="register-btn" @click="handleRegister">注册</button> -->
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { wxLogin, wxRegister } from '@/api/wxlogin/wxlogin'
import { nurseLogin } from '@/api/system/login'
import { useUserStore } from '@/store'

const userStore = useUserStore()

// 登录表单数据
const loginForm = ref({
  userName: '',
  password: ''
})

// 注册表单数据
const registerForm = ref({
  userName: '',
  password: ''
})

// 处理登录
const handleLogin = async () => {
  if (!loginForm.value.userName || !loginForm.value.password) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    })
    return
  }

  try {
    // const res = await wxLogin(loginForm.value)
    // const userId = res.data.userId
    // uni.setStorageSync('userId', userId)
    const res = await nurseLogin({
        archiveCode: loginForm.value.userName,
        password: loginForm.value.password
      });
    if (res.code === 200) {
      // 登录成功处理
      const userInfo = res.data
      uni.setStorageSync('archiveId', userInfo.archiveId)
      userStore.login(res.data)
      uni.setStorageSync('nurseisLoggedIn', true)
      uni.setStorageSync('nurseuserInfo', JSON.stringify(userInfo))
      console.log("userInfo",userInfo)
      // 跳转到首页
      uni.navigateTo({
        url: '/pages/nurse-platform/index/index'
      })
// //页面回退
//       uni.navigateBack()
      uni.showToast({
        title: '登录成功',
        icon: 'success'
      })
    } else {
      throw new Error(res.msg || '登录失败')
    }
  } catch (err) {
    console.error('登录失败:', err)
    uni.showToast({
      title: err.message || '登录失败',
      icon: 'none'
    })
  }
}

// 处理注册
const handleRegister = async () => {
  if (!registerForm.value.userName || !registerForm.value.password) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    })
    return
  }

  try {
    const res = await wxRegister(registerForm.value)
    const userId = res.data.userId
    uni.setStorageSync('userId', userId)
    if (res.code === 200) {
      uni.showToast({
        title: '注册成功',
        icon: 'success'
      })
      // 清空注册表单
      registerForm.value = {
        userName: '',
        password: ''
      }
    } else {
      throw new Error(res.msg || '注册失败')
    }
  } catch (err) {
    console.error('注册失败:', err)
    uni.showToast({
      title: err.message || '注册失败',
      icon: 'none'
    })
  }
}

const handleReturn = () => {
  uni.navigateBack();
};
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.head {
  width: 100%;
  background-color: #ffcc00;
  height: 180rpx;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.form-container {
  width: 100%; /* 覆盖整个宽度 */
  height: calc(100vh - 180rpx); /* 减去头部高度，覆盖剩余高度 */
  background-color: #fff; /* 背景颜色 */
  padding: 40rpx; /* 内边距 */
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1); /* 阴影效果 */
  margin-top: 180rpx; /* 与头部保持距离 */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 40rpx;
  color: #333;
  text-align: center;
}

.form-item {
  margin-bottom: 30rpx;
  width: 80%; /* 输入框宽度 */
}

.input {
  width: 100%;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 12rpx; /* 圆角 */
  font-size: 28rpx;
  background-color: #f9f9f9;
  transition: border-color 0.3s ease; /* 聚焦时边框颜色变化 */

  &:focus {
    border-color: #007aff; /* 聚焦时边框颜色 */
  }
}

.login-btn {
  width: 80%; /* 按钮宽度 */
  height: 80rpx;
  line-height: 80rpx;
  font-size: 32rpx;
  color: #fff;
  border-radius: 12rpx; /* 圆角 */
  margin-top: 20rpx;
  background-color: #007aff;
  border: none;
  transition: background-color 0.3s ease;

  &:hover {
    background-color: #005bb5; /* 鼠标悬停时背景颜色 */
  }
}

.register-btn {
  background-color: #4cd964;
}

.image-container {
  display: flex;
  justify-content: center;
  margin-bottom: 30rpx;
}

.profile-image {
  width: 150rpx;
  height: 150rpx;
  border-radius: 25%;
  object-fit: cover;
  margin-bottom: 30rpx;
}
</style>
